import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public abstract class EntityView implements IObserver
{
  protected Entity entity;
  protected Game game;
  protected View view;
  protected Point pos = null;

  public void draw(Graphics e, Insets in)
  {
    Image img = null;
    if(pos == null && entity.alive != false)
      pos = entity.getPos();

    Graphics2D g2d = (Graphics2D) e;

    try
    {
      img = ImageIO.read(new File(getImageFileName()));
    }
    catch (IOException ex)
    {
      return;
    }
    if(entity.isAlive())
    {
      g2d.drawImage(img, in.left+pos.x*35, in.top + pos.y*42 + (pos.x%2 != 0 ? 21 : 0), null);
    }
  }

  protected abstract String getImageFileName();

  public void setView(View view)
  {
    this.view = view;
  }

  public void setEntity(Entity entity)
  {
    this.entity = entity;
   // pos = game.getPos(entity);
  }

  public void setGame(Game game)
  {
    this.game = game;
  }

  public void update()
  {
    pos = entity.getPos();
    view.invalidate();
  }
}